<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('sex')->display(function ($sex) {
                return $sex == 1 ? '男' : ($sex == 2 ? '女' : '未知');
            });;
            $grid->column('birth');
            $grid->column('avatar')->image('', 100);
            $grid->column('pid');
            $grid->column('balance');
            $grid->column('is_invite')->display(function ($sex) {
                return $sex == 1 ? '是' : ($sex == 0 ? '否' : '未知');
            });;
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
            $grid->export();
            $grid->disableCreateButton();
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('sex');
            $show->field('birth');
            $show->field('avatar');
            $show->field('pid');
            $show->field('balance');
            $show->field('is_invite');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->display('name');
            $form->display('sex');
            $form->display('birth');
            $form->image('avatar')->disable();
            $form->display('pid');
            $form->display('balance');
            $is_inviet = [
                '0' => '否',
                '1' => '是',
            ];
            $form->select('is_invite')->options($is_inviet);
            $form->display('created_at');
            $form->display('updated_at');
            $form->disableViewButton();
        });
    }
}
